<?php

namespace Common\Model;

use Think\Model;

class UserCouponModel extends Model {

    protected $_validate = array(/* array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]) */
        array('userID', 'require', '用户ID！', 1),
        array('couponID', 'require', '券ID！', 1),
    );  // 自动验证定义
    protected $_auto = array(/* array(完成字段1,完成规则,[完成条件,附加规则]) */
        array('addTime', 'time', 1, 'function'),
        array('updateTime', 'time', 2, 'function'),
        array('isOn', '1', 1),
        array('status', '0', 1),
    );  // 自动完成定义

    /**
     * 添加
     */

    public function addUserCoupon($data = '') {
        $rt = false;
        if ($this->create($data)) {
            $rt = $this->add();
        }
        return $rt;
    }

    /**
     * 更新(注意$data一定要有主键id,若$data为空，则默认是对$_POST数组处理)
     */
    public function updateUserCoupon($data = '') {
        $rt = false;
        if ($this->create($data)) {
            $rt = $this->save();
        }
        return $rt;
    }

    /**
     * 删除
     */
    public function deleteUserCoupon($id) {
        $data = array(
            'userCouponID' => $id,
            'isOn' => 0
        );
        return $this->updateUserCoupon($data);
    }

    /**
     *  获取一条记录
     */
    public function getUserCoupon($id, $where = array()) {
        if (!empty($id)) {
            $where['userCouponID'] = $id;
        }

        $result = $this->where($where)->find();

        return $result;
    }

    /**
     * 获得列表
     */
    public function getUserCouponList($pageNo = 1, $pageSize = 15, $userID = 0, $order = "",$where) {
		$where['a.userID']=$userID;
    	
        $rt = M("user_coupon a")
                        ->field("a.userCouponID,b.couponName")
                        ->join(C("DB_PREFIX") . "coupon b on a.couponID=b.couponID")
                        ->where($where)
                        ->page("$pageNo,$pageSize")->order($order)->select();
        return $rt;
    }

    public function getUserCouponLength($where = array()) {
        return $this->where($where)->count();
    }

    /**
     * 获得已发布的用户列表
     */
    public function getcouponsUserList($pageNo = 1, $pageSize = 15, $couponID = 0) {

        $rt = $this->field(C("DB_PREFIX") . "user_coupon.userCouponID ucID," .C("DB_PREFIX") . "user.phone uPhone," . C("DB_PREFIX") . "user.email uEmail," . C("DB_PREFIX") . "user.nickName uNickName,"
                                . C("DB_PREFIX") . "user_coupon.addTime publicTime," . C("DB_PREFIX") . "user_coupon.status couponStatus," . C("DB_PREFIX") . "user_coupon.useTime useTime")
                        ->join(C("DB_PREFIX") . "user ON " . C("DB_PREFIX") . "user_coupon.userID = " . C("DB_PREFIX") . "user.userID", 'LEFT')
                        ->where(C("DB_PREFIX") . "user_coupon.couponID = $couponID and ".C("DB_PREFIX") . "user_coupon.isOn = 1")
                        ->page("$pageNo,$pageSize")->order(C("DB_PREFIX") . "user_coupon.addTime desc")->select();
        return $rt;
    }

    public function getcouponsUserListLength($coupon = 0) {
        if ($coupon > 0) {
            $where['couponID'] = $coupon;
        }
        return $this->where($where)->count();
    }

}

?>